﻿#include "widget4.h"
#include "ui_widget4.h"
#include "common.h"
#include <qmath.h>
#include <QRadioButton>
#include <QScreen>
#include <QDebug>

Widget4::Widget4(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget4)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    ui->pageWidget->setPerpageDataCount(m_perpageCnt);
    connect(ui->pageWidget, SIGNAL(sign_pageChanged(int)), this, SLOT(slot_pageChanged(int)));
    // 获取主屏幕的大小
    QScreen *screen = QGuiApplication::primaryScreen();
    QSize screenSize = screen->availableGeometry().size();
    ui->widget->setFixedHeight(screenSize.height()*5/9);
}

Widget4::~Widget4()
{
    delete ui;
}

void Widget4::setShowData(int index)
{
    m_pshowDatasList = regulationtotal.at(index);
    ui->pageWidget->setDataCount(m_pshowDatasList.size());
    int pageCount = qCeil(m_pshowDatasList.size() / (double)m_perpageCnt);
    ui->pageWidget->setPageCount(pageCount);
}

// font 设置的字体属性
// text 文本
// nLabelSize 到达此宽度换行

QString Widget4::ProcessingText(const QFontMetrics& font, const QString& text, int nLabelSize)
{
    int nTextSize = font.width(text);
    if (nTextSize > nLabelSize)
    {
        int nPos = 0;
        long nOffset = 0;
        for (int i = 0; i < text.size(); i++)
        {
            nOffset += font.width(text.at(i));
            if (nOffset > nLabelSize)
            {
                nPos = i;
                break;
            }
        }

        QString qstrLeftData = text.left(nPos);
        QString qstrMidData = text.mid(nPos);
        return qstrLeftData + "\n" + ProcessingText(font, qstrMidData, nLabelSize);
    }
    return text;
}

void Widget4::slot_pageChanged(int)
{
    int offset = ui->pageWidget->perpageDataCount() * (ui->pageWidget->currentPage() - 1);
    QList<QPair<QString,QString>> currPageDataList = m_pshowDatasList.mid(offset, ui->pageWidget->perpageDataCount());

    clearLayout(ui->verticalLayout);
    for (auto item : currPageDataList)
    {
        QPushButton* btn = new QPushButton();
        btn->setObjectName("widget4_button");
        QScreen *screen = QGuiApplication::primaryScreen();
        QSize screenSize = screen->availableGeometry().size();
        int index = item.first.indexOf(QStringLiteral("条"));
        QString str = "";
        if(index < 8)
        {
            str = item.first.mid(index+2);
        }
        str = ProcessingText(QFontMetrics(btn->font()),str,screenSize.width()/2.3);
        btn->setText(str);
        btn->setProperty("texts", item.first);
        btn->setProperty("result", item.second);
        connect(btn,&QPushButton::clicked,this,&Widget4::SlotRadioBtnToggle);
        ui->verticalLayout->addWidget(btn);
    }
}

void Widget4::SlotRadioBtnToggle(bool)
{
    QPushButton* btn = qobject_cast<QPushButton*>(sender());
    emit SignalWidget4Next(qMakePair(btn->property("texts").toString(),btn->property("result").toString()));
}

void Widget4::clearLayout(QLayout *layout)
{
    QLayoutItem* item = nullptr;
    while ((item = layout->takeAt(0)))
    {
        if(item->layout())
        {
            clearLayout(item->layout());
        }
        if(item->widget())
        {
            delete item->widget();
        }
        delete item;
    }
}
